package com.gjm.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

public class JsonTestDemo {

    @Test
    public void testObjectToJson() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //创建java对象
        UserInfo userInfo = new UserInfo("张三", 12,1);
        //对象转json
        String ret = objectMapper.writeValueAsString(userInfo);
        System.out.println(ret);
    }

    @Test
    public void testJsonToObject() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //定义json字符串
        String str = "{\"userName\":\"张三\", \"age\":\"12\", \"gender\":\"1\"}";
        UserInfo userInfo = objectMapper.readValue(str, UserInfo.class);
        System.out.println(userInfo.toString());
    }
}
